$(document).ready(function() {
			updateMainBlock();
		});

function init(type) {
	$("*").unbind();
	$(":checked").each(function() {
				$(this).removeAttr("checked");
			});
	$("#deleteInput").attr("disabled", "disabled");
	initTypeSelector(type);// 初始化类型选择器
	$("#allTitle").bind("click", function() {
				if ($("#allTitle:checked").length == 1) {
					$(":checkbox").each(function() {
								$(this).attr("checked", "checked");
							});
				} else {
					$(":checkbox").each(function() {
								$(this).removeAttr("checked");
							});
				}
			});
}

/* 初始化类型选择器 */
function initTypeSelector(type) {
	$("#typeSelector").children("option").each(function() {
				if ($(this).attr("value") == type) {
					$(this).attr("selected", "selected");
				} else {
					$(this).removeAttr("selected");
				}
			});
	$("#typeSelector").bind("change", function() {
				var type = $("#typeSelector").val();
				updateTitles(type, 1, 8);
			});
}

function isAllChecked() {
	return $(".title:checkbox").length == $(".title:checked").length;
}

function getCheckeds() {
	var ids = new Array();
	$(".title:checked").each(function() {
				var id = $(this).attr("value");
				ids.push(id);
			});
	return ids;
}

function deleteArticles() {
	if (confirm("Confirm Delete?")) {
		var ids = getCheckeds().join(",");
		$.getJSON("../../DeleteArticle", {
					ids : ids
				}, function(result) {
					if (result == 0) {
						alert("delete wrong");
					} else if (result > 0) {
						updateTitles(1, 1, 8);
					}
				})
	}
}

/**
 * 更新文章列表
 * 
 * @param {}
 *            type 类型 0：中文版 1：英文版
 * @param {}
 *            currentPage 当前页
 * @param {}
 *            pageSize 每页显示数目
 */
function updateTitles(type, currentPage, pageSize) {
	init(type);// 初始化页面
	window.location.href = "#type=" + type + "&currentPage=" + currentPage
			+ "&pageSize=" + pageSize;
	var updateTitleTable = function(titles) {
		$("#titleList").html("");
		if (titles.length == 0) {
			$("#titleList")
					.append("<tr><td colspan='3' style='text-align:center;'>NO RECORD</td></tr>");
		}
		for (var i = 0; i < titles.length; i++) {
			$("#titleList")
					.append("<tr><td style='text-align:center;'><input type='checkbox' class='title' value='"
							+ titles[i].id
							+ "'/></td><td style='text-align:center'><span>"
							+ titles[i].id
							+ "</span><input type='button' class='modify' value='Modify' style='float:right'/></td><td ><a class='thickbox' target='_blank' href='../news.jsp#id="
							+ titles[i].id
							+ "'>"
							+ titles[i].title
							+ "</a></td></tr>");
		}
		$(".modify").bind("click", function() {
					var url = "../../DataTrans?id="
							+ $(this).prev("span").text();
					window.open(url);
				});
	};
	var updatePage = function(page) {
		$("#totalPages").html(page.totalPages);
		$("#currentPage").html(page.currentPage);
		$("#pageUp").unbind("click");
		$("#pageDown").unbind("click");
		if (page.currentPage == 1) {
			$("#pageUp").attr("disabled", "disabled");
		} else {
			$("#pageUp").removeAttr("disabled");
			$("#pageUp").bind("click", function() {
						updateTitles(type, page.currentPage - 1, page.pageSize);
					});
		}
		if (page.currentPage == page.totalPages) {
			$("#pageDown").attr("disabled", "disabled");
		} else {
			$("#pageDown").removeAttr("disabled");
			$("#pageDown").bind("click", function() {
						updateTitles(type, page.currentPage + 1, page.pageSize);
					});
		}
	}
	$("#loader").trigger("click");
	$.getJSON("../../ShowTitle", {
				type : type,
				currentPage : currentPage,
				pageSize : pageSize
			}, function(result) {
				updatePage(result.page);
				updateTitleTable(result.titles);
				/* 只有选择到的情况下，删除才有效 */
				$(":checkbox").bind("click", function() {
							if (!isAllChecked()) {
								$("#allTitle").removeAttr("checked");
							} else {
								$("#allTitle").attr("checked", "checked");
							}
							if ($(":checked").length == 0)
								$("#deleteInput").attr("disabled", "disabled");
							else
								$("#deleteInput").removeAttr("disabled");
						});
				$("#loaderCloser").trigger("click");
			});
}

/**
 * 根据域名进行更新
 */
function updateMainBlock() {
	var url = window.location.href;
	var startIndex = url.indexOf("#") + 1;
	if (startIndex == 0) {
		updateTitles(1, 1, 8);
	}
	var subUrl = url.substring(startIndex);
	var params = subUrl.split("&");
	var type = params[0].substring(params[0].search("[0-9]+"));
	var currentPage = params[1].substring(params[1].search("[0-9]+"));
	var pageSize = params[2].substring(params[2].search("[0-9]+"));
	updateTitles(type, currentPage, pageSize);
}
